package eabnp.eflow;

import eabnp.basic.property.EPropertyMap;
import eabnp.basic.result.GResult;
import eabnp.basic.result.IResult;
import eabnp.basic.result.SGResult;
import eabnp.basic.result.SimpleResult;
import eabnp.basic.util.ClassUtil;
import eabnp.basic.util.DateTimeUtil;
import eabnp.basic.util.StringUtil;
import eabnp.eflow.esession.cache.IESessionCacheMan;
import eabnp.eflow.esession.cache.impl.hb.ESessionCacheManByHB;
import eabnp.eflow.info.ctrlinfo.sysctrl.SysCtrlInfo;
import eabnp.eflow.info.ctrlinfo.sysctrl.UserCtrlInfo;
import eabnp.eflow.info.ctrlinfo.sysctrl.basicinfo.UserInfo;
import eabnp.eflow.misc.UrgentLevel;
import eabnp.eflow.misc.app2eflow.App2EFlowProcess;
import eabnp.eflow.operation.EFlowCommand;
import eabnp.eflow.quatz.EFlowQuatz;
import eabnp.eflow.server.IEFlowServer;
import eabnp.eflow.server.impl.hb.EFlowServerByHB;
import eabnp.eflow.session.ESession;
import eabnp.eflow.sso.ISSO;
import eabnp.eflow.tag.EFlowElementType;
import eabnp.eoperation.EOperationByMap;
import eabnp.eoperation.IEOperation;
import eabnp.service.AbsService;
import eabnp.service.ServiceManager;
import eabnp.service.elog.ILogger;
import java.util.Date;

public class EFlow extends AbsService
{
  public static final String SERVICE_NAME = "EFlow";
  public static final String PROPERTY_NAME_SERVER_CLASS = "Managerr.Class";
  public static final String DEFAULT_ADMIN_CLASS = EFlowServerByHB.class
    .getName();
  public static final String PROPERTY_NAME_LOGIN_IMPLEMENT = "Login.Implement";
  public static final String PROPERTY_NAME_LOGIN_IMPLEMENT_MANAGER = "Manager";
  public static final String PROPERTY_NAME_LOGIN_CLASS = "Class";
  private static final String EVAR_NAME_TRIGGER1 = "BackSideTrigger1";
  private static final String EVAR_NAME_TRIGGER2 = "BackSideTrigger2";
  private IEFlowServer server;
  private static IESessionCacheMan sessionCacheMan = new ESessionCacheManByHB();

  private static EFlow instance = new EFlow();

  public static final EFlow getInstance()
  {
    return instance;
  }

  public static String getEVarString(String code)
  {
    return ServiceManager.getEVarString("EFlow", code);
  }

  public static int getProperDepartmentLevel(int level)
  {
    if ((level <= 0) || (level > 4)) {
      return 1;
    }
    return level;
  }

  public static SGResult loginAndGetSysCtrlInfo(String userUN, String sysUN, String password)
  {
    SGResult sr = loginValidate(userUN, password, null);
    if (sr.isFailure()) return sr;

    SGResult sr1 = getSysCtrlInfo(userUN, sysUN);
    if (sr1.isFailure()) return sr1;

    ((ESession)sr1.getResultObject()).setSingleSignOn((ISSO)
      sr.getResultObject());

    return sr1;
  }

  public static SGResult loginValidate(String userUN, String password, EPropertyMap paraMap)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow没有启动").addErrMsgPreStr(
        "〖" + userUN + "〗登录验证失败");
    }

    IResult r = instance.server.loginValidate(userUN, password, paraMap);
    if (r.isFailure()) {
      getInstance().getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getSysCtrlInfo(String userUN, String sysUN)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getSysCtrlInfo(userUN, sysUN);
    if (r.isFailure()) {
      getInstance().getMyLogger().logErr(r);
      return r.toSGResult();
    }
    SGResult sr = r.toSGResult();
    SysCtrlInfo ctrlInfo = (SysCtrlInfo)sr.getSerializableResultObject();
    r = sessionCacheMan.putESessionToCache(ctrlInfo);
    if (r.isFailure()) {
      getInstance().getMyLogger().logErr(r);
    }
    return r.toSGResult();
  }

  public static SGResult getSysCtrlInfo(String sessionId)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) {
      getInstance().getMyLogger().logErr(r);
    }
    return r.toSGResult();
  }

  public static SGResult loginValAndGetUserInfo(String userUN, String password, EPropertyMap paraMap)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow没有启动").addErrMsgPreStr(
        "〖" + userUN + "〗登录验证失败");
    }

    IResult r = instance.server.loginValAndGetUserInfo(userUN, password, 
      paraMap);
    if (r.isGeneralResult()) getInstance().getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public String getName()
  {
    return "EFlow";
  }

  public static void backSideWork1()
  {
    ILogger logger = getInstance().getMyLogger();
    logger.logSysInfo("开始清理过期会话缓存");
    IResult r = sessionCacheMan.clearOuttimeESession();
    if (r.isFailure())
      logger.logErr(r);
    else {
      logger.logSysInfo("清理过期会话缓存执行完毕");
    }
    logger.logSysInfo("开始清理流程过程");
    r = getInstance().server.checkProcess();
    if (r.isFailure())
      logger.logErr(r);
    else {
      logger.logSysInfo("清理流程过程执行完毕");
    }
    logger.logSysInfo("开始清理待办已办");
    r = getInstance().server.checkTodoDone();
    if (r.isFailure())
      logger.logErr(r);
    else
      logger.logSysInfo("清理待办已办执行完毕");
  }

  public static void backSideWork2()
  {
    ILogger logger = getInstance().getMyLogger();
    logger.logSysInfo("开始处理超过办理日期的任务");
    IResult r = getInstance().server.checkTaskUptoTime();
    if (r.isFailure())
      logger.logErr(r);
    else
      logger.logSysInfo("处理超过办理日期的任务执行完毕");
  }

  public IResult startService(boolean force) {
    String errMsg = "启动服务EFlow失败";

    if ((this.active) && (!force)) {
      return SimpleResult.SUCCESS;
    }

    this.active = false;

    String adminClassName = getProperty("Managerr.Class", 
      DEFAULT_ADMIN_CLASS);

    getMyLogger().logSysInfo("EFlow管理类:" + adminClassName);
    GResult gr = ClassUtil.getInstanceOfClass(adminClassName);
    if (gr.isFailure()) {
      gr.addErrMsgPreStr("获取管理类实例失败").addErrMsgPreStr(errMsg);
      return getMyLogger().logErr(gr);
    }

    Object obj = gr.getResultObject();
    if (!ClassUtil.isInterfaceImplents(obj.getClass(), 
      IEFlowServer.class.getName())) {
      gr.resetFailureResult(adminClassName + "不支持IEFlowManager接口")
        .addErrMsgPreStr(errMsg);
      return getMyLogger().logErr(gr);
    }

    this.server = ((IEFlowServer)obj);
    IResult r = this.server.initialize();
    if (r.isFailure()) {
      return getMyLogger().logErr(r.toGResult().addErrMsgPreStr(errMsg));
    }

    this.active = true;

    getInstance().getMyLogger().logSysInfo("初始化EFlowQuatz");
    String t1 = getEVarString("BackSideTrigger1");
    String t2 = getEVarString("BackSideTrigger2");
    r = EFlowQuatz.getInstance().startScheduler(t1, t2);
    if (r.isFailure())
      getInstance().getMyLogger().logErr(r);
    else {
      getInstance().getMyLogger().logSysInfo("初始化EFlowQuatz成功");
    }

    return SimpleResult.SUCCESS;
  }

  public IResult stopService(boolean force) {
    return null;
  }

  public static IEFlowServer getManager() {
    return instance.server;
  }

  public static IResult operate(ESession sessoin, IEOperation operation) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.operate(sessoin, operation);
    if (r.isFailure()) {
      getInstance().getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getOrgArray()
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    return instance.server.getOrgArray();
  }

  public static SGResult getOrgOUTree(String orgUN)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getOrgOUTree(orgUN, true);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getSysTree(String sysUN)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    return instance.server.getSysTree(sysUN);
  }

  public static SGResult genEFlowXMLFile() {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.genEFlowXMLFile();
    if (r.isFailure()) instance.getMyLogger().logErr(r);

    return r.toSGResult();
  }

  public static SGResult genEFlowXMLFile(String orgCodeList) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.genEFlowXMLFile(orgCodeList);
    if (r.isFailure()) instance.getMyLogger().logErr(r);

    return r.toSGResult();
  }

  public static SGResult getUserArray(EPropertyMap paraMap) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getUserArray(paraMap);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getInfo(String uniqueName) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getInfo(uniqueName);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getSysUserArray(String sysUN) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getSysUserArray(sysUN);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getUserSysArray(String sysUN) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getUserSysArray(sysUN);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getSysAppTree(String sysUN) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getSysAppTree(sysUN);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getSysAppFlowTree(String sysUN) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getSysAppFlowTree(sysUN);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getOrgAppTree(String orgUN) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getOrgAppTree(orgUN);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getOrgAppFlowTree(String orgUN) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getOrgAppFlowTree(orgUN);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getAppTree(String appUN) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getAppTree(appUN);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static IResult delete(String uniqueName) {
    IEOperation op = new EOperationByMap();
    op.setCommand(EFlowCommand.DELETE);
    op.setParameter("un", uniqueName);
    op.setParameter("deleteType", "un");
    IResult r = instance.server.operate(null, op);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
      return r.toSGResult();
    }

    return r;
  }

  public static IResult deleteById(String id, EFlowElementType element)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.deleteById(id, element);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getOrgGroupTree(String orgUN) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getOrgGroupTree(orgUN);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult launchProcessToStep(String sessionId, String flowUN, String stepCode, String title, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.launchProcessToStep(es, flowUN, stepCode, 
      title, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult launchProcess(String sessionId, String flowUN, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.launchProcess(es, flowUN, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getProcesCommonInfo(String sessionId, String processId, String infoType)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server
      .getProcessSimpleInfo(es, processId, infoType);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult loadProcess(String sessionId, String processId, String userUN, String title, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.loadProcess(es, processId, userUN, title, 
      appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult visitEFlowPostAct(String sessionId, String processId, String taskId, String actId, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.visitEFlowPostAct(es, processId, taskId, 
      actId, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult visitEFlowPreAct(String sessionId, String processId, String taskId, String actId)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.visitEFlowPreAct(es, processId, taskId, 
      actId);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getAutoSendConfirm(String sessionId, String processId, String taskId, String actId, String theOUByApp, String theUserByApp)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.getAutoSendConfirm(es, processId, taskId, 
      actId, theOUByApp, theUserByApp);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult autoSend(String sessionId, String processId, String taskId, String actId, String theOUByApp, String theUserByApp, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.autoSend(es, processId, taskId, actId, 
      theOUByApp, theUserByApp, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getSendCtrlInfo(String sessionId, String processId, String taskId, String actId, String theOUByApp, String theUserByApp, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.getSendCtrlInfo(es, processId, taskId, 
      actId, theOUByApp, theUserByApp, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult selSend(String sessionId, String processId, String taskId, String sendStr, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.selSend(es, processId, taskId, sendStr, 
      appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult putComment(String sessionId, String processId, String taskId, String actId, String actUserUN, String comment, String commentDate, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    Date date = null;
    if (StringUtil.isNullOrEmptyStr(commentDate)) {
      date = DateTimeUtil.getNow();
    } else {
      date = DateTimeUtil.parse(commentDate);
      if (date == null)
      {
        return SGResult.newFailureResult("日期“" + commentDate + "失败")
          .addErrMsgPreStr("填写意见操作失败");
      }
    }

    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.putComment(es, processId, taskId, actId, 
      actUserUN, comment, date, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getCurComment(String sessionId, String processId, String taskId, String actId, String actUserUN)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.getCurComment(es, processId, taskId, actId, 
      actUserUN);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getPutCommentCtrl(String sessionId, String processId, String taskId, String actId, String actUserUN)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.getPUtCommentCtrl(es, processId, taskId, 
      actId, actUserUN);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getFlowXMLFile(String flowUN, int flowVersion)
  {
    IResult r = getInstance().server.getFlowXMLFile(flowUN, flowVersion);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getProcessRecordInfo(String processId)
  {
    IResult r = getInstance().server.getProcessRecordInfo(processId);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getProcessInfo(String id, EFlowElementType rootType, EFlowElementType childrenType)
  {
    IResult r = getInstance().server.getProcessInfo(id, rootType, 
      childrenType);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult goBack(String sessionId, String processId, String actId, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.goBack(es, processId, actId, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult autoTakeback(String sessionId, String processId, String actId, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.autoTakeback(es, processId, actId, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getGobackAutoConfirm(String sessionId, String processId, String actIdEx)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.getGobackAutoConfirm(es, processId, actIdEx);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getTakebackSelCtrl(String sessionId, String processId, String actIdEx)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.getTakebackSelCtrl(es, processId, actIdEx);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult selTakeback(String sessionId, String processId, String actIdEx, String userList, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.selTakeback(es, processId, actIdEx, 
      userList, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getResendListCtrl(String sessionId, String processId, String actIdEx, String theOUByApp, String theUserByApp, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.getResendListCtrl(es, processId, actIdEx, 
      theOUByApp, theUserByApp, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getUN_NameArray(String prefix, String postfix)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = getInstance().server.getUN_NameArray(prefix, postfix);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult addUserUsualComment(String sessionId, String userUN, String sysCode, String comment)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    if (userUN == null)
      userUN = es.getSysCtrlInfo().getUserCtrl().getUserInfo().getUniqueName();
    r = getInstance().server.addUserUsualComment(es, userUN, sysCode, 
      comment);
    if (r.isFailure())
      instance.getMyLogger().logErr(r);
    else if ((userUN != null) && 
      (userUN.equals(
      es.getSysCtrlInfo().getUserCtrl().getUserInfo().getUniqueName()))) sessionCacheMan.updateESessionCache(es);
    return r.toSGResult();
  }

  public static SGResult addUserUsualWord(String sessionId, String userUN, String sysCode, String word)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.addUserUsualWord(es, userUN, sysCode, word);
    if (r.isFailure())
      instance.getMyLogger().logErr(r);
    else if ((userUN != null) && 
      (userUN.equals(
      es.getSysCtrlInfo().getUserCtrl().getUserInfo().getUniqueName()))) sessionCacheMan.updateESessionCache(es);
    return r.toSGResult();
  }

  public static SGResult resetUserUsualComment(String sessionId, String userUN, String sysCode, String commentArray)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    if (StringUtil.isNullOrEmptyStr(userUN)) {
      userUN = es.getSysCtrlInfo().getUserCtrl().getUserInfo().getUniqueName();
    }
    r = getInstance().server.resetUserUsualComment(es, userUN, sysCode, 
      commentArray);
    if (r.isFailure())
      instance.getMyLogger().logErr(r);
    else if ((userUN != null) && 
      (userUN.equals(
      es.getSysCtrlInfo().getUserCtrl().getUserInfo().getUniqueName()))) sessionCacheMan.updateESessionCache(es);
    return r.toSGResult();
  }

  public static SGResult resetUserUsualWord(String sessionId, String userUN, String sysCode, String wordArray)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.resetUserUsualWord(es, userUN, sysCode, 
      wordArray);
    if (r.isFailure())
      instance.getMyLogger().logErr(r);
    else if ((userUN != null) && 
      (userUN.equals(
      es.getSysCtrlInfo().getUserCtrl().getUserInfo().getUniqueName()))) sessionCacheMan.updateESessionCache(es);
    return r.toSGResult();
  }

  public static SGResult resend(String sessionId, String processId, String sendStr, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.resend(es, processId, sendStr, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult setProcessAppData(String sessionId, String processId, String appData)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.setProcessAppData(es, processId, appData);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult setProcessTitle(String sessionId, String processId, String title)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();

    r = getInstance().server.setProcessTitle(es, processId, title);
    if (r.isFailure()) instance.getMyLogger().logErr(r);
    return r.toSGResult();
  }

  public static SGResult getProcessArray(EPropertyMap paraMap) {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getProcessArray(paraMap);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getFlowStepArray(String flowUN, int flowVersion, String stepType)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    IResult r = instance.server.getFlowStepArray(flowUN, flowVersion, stepType);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getTodoList(String sessionId, String sysCodeList, String labelList, int first, int count)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.getTodoList(es, sysCodeList, labelList, first, count);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getDoneLabelList(String sessionId, String sysCodeList, Date fromDate, Date toDate)
  {
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.getDoneLabelList(es, sysCodeList, fromDate, toDate);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult getTodoLabelList(String sessionId, String sysCodeList)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.getTodoLabelList(es, sysCodeList);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult newAgent(String sessionId, String sysUN, String agentUserUN, String beginDateStr, String endDateStr, String desc)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    Date beginDate = null;
    if (StringUtil.isNotNullEmptyStr(beginDateStr)) {
      beginDate = DateTimeUtil.parse(beginDateStr);
      if (beginDate == null) {
        return SGResult.newFailureResult("日期格式错误：" + beginDateStr);
      }
    }

    Date endDate = null;
    if (StringUtil.isNotNullEmptyStr(endDateStr)) {
      endDate = DateTimeUtil.parse(endDateStr);
      if (endDate == null) {
        return SGResult.newFailureResult("日期格式错误：" + endDateStr);
      }
    }

    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.newAgent(es, sysUN, agentUserUN, beginDate, endDate, 
      desc);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult addTodo(String sessionId, String processId, String senderCode, String senderName, String userCode, String userName, String label, String tip, String title, String link, String exInfo, int urLevel)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    UrgentLevel level = UrgentLevel.LEVEL0;
    try {
      level = UrgentLevel.valueOf(urLevel);
    }
    catch (Exception localException) {
    }
    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.addTodo(es, processId, senderCode, senderName, 
      userCode, userName, label, tip, title, link, exInfo, level);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult resetTodoInfo(String sessionId, String processId, String title, String link, String exInfo)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.resetTodoInfo(es, processId, title, link, exInfo);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult delDone(String sessionId, String doneIdList)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.delDone(es, doneIdList);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult delTodo(String sessionId, String todoIdList)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.delTodo(es, todoIdList);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult setDone(String sessionId, String todoIdList)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.setDone(es, todoIdList);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult exportApp(String appUN)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    return instance.server.exportApp(appUN).toSGResult();
  }

  public static SGResult exportOrg(String orgUN)
  {
    if (!instance.active) {
      return SGResult.newFailureResult("EFlow服务未激活");
    }

    return instance.server.exportOrg(orgUN).toSGResult();
  }

  public static SGResult getDoneList(String sessionId, String sysCodeList, String labelList, Date fromDate, Date toDate, int first, int count)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.getDoneList(es, sysCodeList, labelList, fromDate, 
      toDate, first, count);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult importAppProcess(App2EFlowProcess appProcess)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");

    IResult r = instance.server.importAppProcess(appProcess);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }
    return r.toSGResult();
  }

  public static SGResult getBatchSendCtrl(String sessionId, String processIdList, String stepCode, String actNameList, String theOUByApp, String theUserByApp)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.getBatchSendCtrl(es, processIdList, stepCode, 
      actNameList, theOUByApp, theUserByApp);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult batchSend(String sessionId, String processIdList, String stepCode, String actName, String sendStr)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server
      .batchSend(es, processIdList, stepCode, actName, sendStr);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult isProcessedErasable(String esId, String processIdList)
  {
    return SGResult.newSuccessResult();
  }

  public static SGResult getUserAgentInfo(String esId)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(esId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.getUserAgentInfo(es);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult delAgent(String sessionId, String agentId)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");
    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.delAgent(es, agentId);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }

  public static SGResult setAgent(String sessionId, String agentId, String sysUN, String agentUserUN, String beginDateStr, String endDateStr, String desc)
  {
    if (!instance.active) return SGResult.newFailureResult("EFlow服务未激活");

    Date beginDate = null;
    if (StringUtil.isNotNullEmptyStr(beginDateStr)) {
      beginDate = DateTimeUtil.parse(beginDateStr);
      if (beginDate == null) {
        return SGResult.newFailureResult("日期格式错误：" + beginDateStr);
      }
    }

    Date endDate = null;
    if (StringUtil.isNotNullEmptyStr(endDateStr)) {
      endDate = DateTimeUtil.parse(endDateStr);
      if (endDate == null) {
        return SGResult.newFailureResult("日期格式错误：" + endDateStr);
      }
    }

    IResult r = sessionCacheMan.getESessionFromCache(sessionId);
    if (r.isFailure()) return r.toSGResult();

    ESession es = (ESession)r.toSGResult().getSerializableResultObject();
    r = instance.server.setAgent(es, agentId, sysUN, agentUserUN, beginDate, 
      endDate, desc);
    if (r.isFailure()) {
      instance.getMyLogger().logErr(r);
    }

    return r.toSGResult();
  }
}